<?php
/*
 * huofu: it is a utility used for shorten url for bitly.
 */
/* make a URL small  by cURL */
function make_bitly_curl($url, $login, $appkey, $format=’xml’, $history=1, $version='2.0.1')
{
	//create the URL
	$bitly = 'http://api.bit.ly/shorten';
	$param = 'version='.$version.'&longUrl='.urlencode($url).'&login='
	.$login.'&apiKey='.$appkey.'&format='.$format.'&history='.$history;

	//get the url
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $bitly . '?' . $param);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$response = curl_exec($ch);
	curl_close($ch);

	//parse depending on desired format
	if(strtolower($format) == 'json') {
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	} else {
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

/* make a URL small  */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
	//create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

	//get the url
	//could also use cURL here
	$response = file_get_contents($bitly);

	//parse depending on desired format
	if(strtolower($format) == 'json')
	{
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	}
	else //xml
	{
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}



